package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.Arrays;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 *
 * @author itheima
 * @date 2025-03-03
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id) {
        // 1. 根据id查询护理计划
        NursingPlan plan = getById(id);

        // 2. 属性拷贝 nursingPlan到 nursingPlanVo中
        // TODO:  BeanUtils的拷贝必须 属性名 + 类型都一致才能拷贝。
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(plan, vo);

        // 3. 根据护理计划id查询关联的所有护理项目计划表的list
        List<NursingProjectPlanVo> projectPlans = nursingProjectPlanMapper.selectByPlanId(id);

        // 4. 将list赋值给nursingPlanVo
        vo.setProjectPlans(projectPlans);

        // 5. 返回nursingPlanVo
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }



    /**
     * 新增护理计划
     * <p>
     * 3.2 将原本的单表保存改造为多表关联保存：
     * a. 护理计划表
     * b. 护理计划和护理项目关联表
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {

        // 1. 属性拷贝dto到NursingPlan对象
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan); // 可以打断点看下这个属性拷贝的效果

        // 2. 保存护理计划到 NursingPlan表
        save(nursingPlan);

        // 3. 批量保存护理项目计划关系 到 nursingProjectPlan表: 注意需要保存 plan_id
        return nursingProjectPlanMapper.batchSave(nursingPlanDto.getProjectPlans(), nursingPlan.getId());

        // 4. 涉及多个表的插入，需要事务控制。
    }


    /**
     * 修改护理计划:
     * <p>
     * 3.2 修改护理计划：多表关联修改
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        //1.属性拷贝：dto拷贝到nursingPlan中
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);

        //2.判断dto中的项目列表是否为空,如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
        if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
            //2.1 删除护理计划对应的护理项目列表
            nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
            //2.2 批量保存护理计划对应的护理项目列表
            nursingProjectPlanMapper.batchSave(dto.getProjectPlans(), dto.getId());
        }

        //3.修改护理计划
        return updateById(nursingPlan) ? 1 : 0;


        //4.注意事务控制
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }







    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Integer id) {

        // 1. 删除关联表数据
        nursingProjectPlanMapper.deleteByPlanId(id);

        // 2. 删除计划表数据
        return removeById(id) ? 1 : 0;

        // 3. 注意事务控制
    }










}